package com.nl.controller.web.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nl.controller.common.annotation.SystemServiceLog;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.service.CommonService;
import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.web.bean.SmsContent;
import com.nl.controller.web.mapper.SmsContentMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class SmsContentService {
    @Autowired
    private SmsContentMapper smsContentMapper;

    @Autowired
    private CommonService commonService;

   // @SystemServiceLog(description = "短信内容管理", value = false)
    public PageInfo<SmsContent> findContent(int page, int limit, String cityId, String mgrId,String mgrName) {
        Map<String, Object> map = new HashMap<>();
        map.put("cityId", cityId);
        map.put("mgrId", mgrId);
        map.put("mgrName", mgrName);
        PageHelper.startPage(page, limit);
        PageInfo<SmsContent> pageInfo;
        try {
            List<SmsContent> list = smsContentMapper.findContent(map);
            //判断现在发的信息的内容
           for(SmsContent content:list){
                String effectTime = content.getEffectTime();
                if (effectTime != null && !"".equals(effectTime)) {
                    log.info(content.getEffectTime());
                    try {
                        if (this.judgement(content)) {
                            content.setContent(content.getNewContent());
                            content.setNewContent("");
                            content.setCreateTime(content.getEffectTime());
                            content.setEffectTime("");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            commonService.createSmsOperLog("4", "短信项目内容查询", "0", "短信内容管理");
            pageInfo = new PageInfo<>(list);
        } catch (Exception e) {
            commonService.createSmsOperLog("4", "短信项目内容查询", "1", "短信内容管理");
            e.printStackTrace();
            log.error("短信项目内容查询：" + cityId);
            throw new SmsException("短信项目内容查询：" + cityId);
        }
        return pageInfo;
    }

   // @SystemServiceLog(description = "短信内容添加", value = false)
    public int addContent(String mgrId, String cityId, String content) {
        int result = 0;
        SmsContent smsContent = new SmsContent();
        smsContent.setCityId(cityId);
        smsContent.setId(mgrId);
        smsContent.setContent(content);
        try {
            result = smsContentMapper.addContent(smsContent);
            commonService.createSmsOperLog("2", "短信内容添加"+mgrId, "0", "短信内容管理");
        } catch (Exception e) {
            e.printStackTrace();
            commonService.createSmsOperLog("2", "短信内容添加"+mgrId, "1", "短信内容管理");
            log.error("短信项目内容添加：" + mgrId);
            throw new SmsException("短信项目内容添加：" + mgrId);
        }
        return result;
    }


   // @SystemServiceLog(description = "查询单个模板内容", value = false)
    public SmsContent findContentByMgrId(String mgrId) {
        SmsContent smsContent = null;
        try {
            smsContent = smsContentMapper.findContentById(mgrId);
            if(smsContent==null){
            	return smsContent;
            }else{
            if (smsContent.getNewContent() != null&&!"".equals(smsContent.getNewContent())) {
            	//判断是否生效
                if (this.judgement(smsContent)) {
                    smsContent.setContent(smsContent.getNewContent());
                }
            }else{
            	smsContent.setContent(smsContent.getContent());
            	}
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("短信项目内容查询：" + mgrId);
            throw new SmsException("短信项目内容查询：" + mgrId);
        }
        return smsContent;
    }


    //@SystemServiceLog(description = "修改模板内容", value = false)
    public int modifyContent(String mgrId, String newContent, String effectTime) {
        int result = 0;
        try {
            SmsContent smsContent = this.findContentByMgrId(mgrId);
            //判断是否第一次修改
            if (smsContent.getNewContent() != null&&!"".equals(smsContent.getNewContent())) {
                //判断是否生效
                if (this.judgement(smsContent)) {
                    smsContent.setContent(smsContent.getNewContent());
                }
            }
            smsContent.setNewContent(newContent);
            smsContent.setId(mgrId);
            smsContent.setEffectTime(effectTime);
            result = smsContentMapper.modifyContent(smsContent);
            commonService.createSmsOperLog("3", "短信内容修改"+mgrId, "0", "短信内容管理");
        } catch (Exception e) {
            e.printStackTrace();
            commonService.createSmsOperLog("3", "短信内容修改"+mgrId, "1", "短信内容管理");
            log.error("短信项目内容修改：" + mgrId);
            throw new SmsException("短信项目内容修改" + mgrId);
        }
        return result;
    }

    /**
     * 判断生效时间时和现在时间的关系
     * true 生效
     * false 未生效
     *
     * @param smsContent
     * @return
     */
    private boolean judgement(SmsContent smsContent) throws Exception {
        String effectTime = smsContent.getEffectTime();
        if (effectTime != null&&!"".equals(effectTime)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = null;
            date = sdf.parse(effectTime);
            Date sysdate = new Date();
            if (date.getTime() < sysdate.getTime()) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    
  //查询操作人号码
    public String findOperatorMsisdn(String mgrId){
    	String operatorMsisdn;
    	try{
         operatorMsisdn = smsContentMapper.findOperatorMsisdn(mgrId);
    	}catch (Exception e) {
			e.printStackTrace();
			 throw new SmsException("操作人号码查询异常："+mgrId);
		}
    	return operatorMsisdn;
    }
}
